package com.cms.manage.util;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;

import com.cms.manage.common.ConmonConfig;


public class StringUtil {
	public static boolean isNullOrEmpty(Object obj) {	
		return obj == null || "".equals(obj.toString().trim()) || "null".equals(obj);
	}
	/**
	 * 获取文件扩展名
	 * @param filename
	 * @return
	 */
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot + 1);
			}
		}
		return filename;
	}
	/**
	 * 获取uuid
	 */
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");

	}
	
	public static boolean checkMobile(String phone){
        String regex = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
        if(phone.length() != 11){
            return false;
        }else{
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if(isMatch){
                return true;
            } else {
            	return false;
            }
        }
	}
	
	/**
	 * 判断是否是图片
	 * @param obj
	 * @return
	 */
	public static boolean notImageFormat(String suffix) {
		// 文件名称为空的场合
		if (StringUtil.isNullOrEmpty(suffix)) {
			return false;
		}
		// 声明图片后缀名数组
		String imgeArray[] = { "bmp","gif", "jpe","jpeg","jpg","png","tiff"};
		// 遍历名称数组
		for (int i = 0; i < imgeArray.length; i++) {
			// 判断单个类型文件的场合
			if (imgeArray[i].equals(suffix.toLowerCase())) {
				return true;
			}
		}
		return false;
	}
	/**
     * 转换为字节数组
     * @param str
     * @return
     */
    public static byte[] getBytes(String str){
    	if (str != null){
    		try {
				return str.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				return null;
			}
    	}else{
    		return null;
    	}
    }
	/**
	 * 字符串去空格判断是否为空
	 * @param obj
	 * @return
	 */
	public static boolean isNullOrEmptyTrim(Object obj) {	
		if(obj == null){
			return true;
		}
		String s = obj.toString().replaceAll(" ", "");
		if("".equals(s)){
			return true;
		}
		return false;
	}
	public static String toString(Object obj){
		if(obj == null) return "";
		return obj.toString();
	}
	/**
	 * 去掉小数点的点和后面的0
	 * @param s
	 * @param delimiter
	 * @return
	 */
	public static String setInteger(String s){
		if(s.indexOf(".") > 0){  
			s= s.replaceAll("0+?$", "");//去掉多余的0  
			s = s.replaceAll("[.]$", "");//如最后一位是.则去掉  
        }
		return s;
	}
	public static String join(Collection s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
	
	 /**
	  * 公有方法转化成字符串
	  * @param obj
	  * @return
	  */
	 public static String convert2String(Object obj){
		String result = "";
		if(obj instanceof String){
			result = (String)obj;
		}else if(obj instanceof Integer){
			result = String.valueOf((Integer)obj);
		}else if(obj instanceof Long){
			result = String.valueOf((Long)obj);
		}
		return result;
	}
 
	 public static void main(String[] args) {	
		System.out.println(DigestUtils.md5Hex(5889+"jellyfy"+ConmonConfig.getKey()).substring(9,21));
			
	}
	public static String formatSqlIn(String id) {
		String str ="";
		String [] ids =id.split(",");
		for (int i = 0; i < ids.length; i++) {
			if(!",".equals(ids[i])){
				str+=ids[i]+",";
			}
		}		
		return str.substring(0, str.length()-1);
	}
	
	
}
